/*
Questions:
What is the output of this program? 
gauge reads 0, safe limit is 20. I believe is susposed to increment to 15 then the remaining threads will
hit the else due to the conditional

susposed to be i believe: gauge reads 15, safe limit is 20
 
Is the pressure over or under the safety limit? 
under bevause the conditional in pressure.RaisePressure wont allow it to go over.

Under

Describe what is going wrong in this code.  What should be the correct output? 

The main thread is exiting before any of the children thread have finished





*/
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class p 
{
	static int pressureGauge = 0;
	static final int safetyLimit = 20;
	static private ExecutorService threadPool  = Executors.newFixedThreadPool(10);




	public static void main(String[] args) throws InterruptedException 
	{
		pressure [] p1= new pressure[10];
		for (int i=0; i < 10; i++) 
		{
			p1[i] = new pressure();
			p1[i].start();
		}
		
		for (int i=0; i < 10; i++) 
			p1[i].join();
	
		System.out.println(
				"gauge reads " + pressureGauge+", safe limit is 20");
	}
}
class pressure extends Thread 
{
	static synchronized void RaisePressure() 
	{
		if (p.pressureGauge < p.safetyLimit-15)
		{
			// wait briefly to simulate some calculations
			try{Thread.sleep(100);} catch (Exception e) {}
			p.pressureGauge += 15;
		} 
		else ; //pressure too high -- don't add to it.
	}
	public void run() {
		RaisePressure();
	}
}